package com.neusoft.databus.core.converter;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.databus.common.dto.MetricGroupDTO;
import com.neusoft.databus.common.enums.Protocol;
import com.neusoft.databus.common.model.Metric;
import com.neusoft.databus.common.model.MetricGroup;
import com.neusoft.databus.core.repository.MetricGroupRepository;
import com.neusoft.databus.core.vo.MetricGroupPortVO;
import com.neusoft.databus.core.vo.MetricPortVO;

/**
 * MetricGroup Model和DTO转换器
 *
 * @author sunchf
 * @date 2018年12月24日 上午9:44:01
 */
@Component
public class MetricGroupConverter extends AbstractConverter<MetricGroup, MetricGroupDTO> {

    @Autowired
    private MetricConverter metricConverter;
    @Autowired
    private MetricGroupRepository metricGroupRepository;
    @Autowired
    private BaseIdGenerator metricGroupCodeGenerator;

    @Override
    public MetricGroup toModel(final MetricGroupDTO dto) {
        MetricGroup model;
        if (dto.isNew()) {
            model = new MetricGroup();
            model.setCode(this.metricGroupCodeGenerator.generateCode());
        } else {
            model = this.metricGroupRepository.getOne(dto.getId());
        }
        model.setName(dto.getName());
        model.setProtocol(dto.getProtocol());
        if (StringUtils.isEmpty(dto.getConfig())) {
            model.setConfig(null);
        } else {
            model.setConfig(dto.getConfig());
        }
        //model.setExecutionCycle(dto.getExecutionCycle());
        model.setUsageType(dto.getUsageType());
        if (dto.getMetrics().size() > 0) {
            model.setMetrics(this.metricConverter.toListModel(dto.getMetrics()));
        }
        model.setComment(dto.getComment());
        return model;
    }

    @Override
    public MetricGroupDTO toDTO(final MetricGroup model, final boolean forListView) {
        final MetricGroupDTO dto = new MetricGroupDTO();
        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setCode(model.getCode());
        dto.setProtocol(model.getProtocol());
        dto.setConfig(model.getConfig());
        //dto.setExecutionCycle(model.getExecutionCycle());
        dto.setUsageType(model.getUsageType());
        final List<Metric> metrics = model.getMetrics();
        dto.setMetricSize(metrics.size());
        if (!forListView) {
            dto.getMetrics()
                    .addAll(metrics.stream().map(m -> this.metricConverter.toDTO(m)).collect(Collectors.toList()));
        }
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());
        dto.setComment(model.getComment());
        return dto;
    }

    public MetricGroup toModel(final MetricPortVO metricPortVO) {
        final MetricGroup model = new MetricGroup();
        model.setCode(this.metricGroupCodeGenerator.generateCode());
        model.setName(metricPortVO.getMetricGroupName());
        model.setProtocol(Protocol.valueOf(metricPortVO.getProtocol().toUpperCase()));
        if (!StringUtils.isEmpty(metricPortVO.getMetricGroupConfig())) {
            model.setConfig(metricPortVO.getMetricGroupConfig());
        }
        return model;
    }

    public MetricGroupPortVO toVO(final MetricGroup model) {
        final MetricGroupPortVO vo = new MetricGroupPortVO();
        vo.setCode(model.getCode());
        vo.setName(model.getName());
        vo.setProtocol(model.getProtocol().toString().toLowerCase());
        return vo;
    }
}
